package com.cloudptt.wp.ui.more.bt;

import java.util.ArrayList;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

public class BtDeviceListAdapter extends BaseAdapter {

	private static final String TAG = "BtDeviceListAdapter";
	private Context ctx;
	private ArrayList<BluetoothDevice> findDeviceList = null;
	private String connectDeviceAddress = null;

	public BtDeviceListAdapter(Context ctx,ArrayList<BluetoothDevice> mFindDeviceList,String mConnectDeviceAddress)
	{
	
		this.ctx = ctx;
		this.findDeviceList = mFindDeviceList;
		this.connectDeviceAddress = mConnectDeviceAddress;
	}
	
	@Override
	public int getCount() {
		return findDeviceList == null ? 0 : findDeviceList.size();
	}

	@Override
	public Object getItem(int position) {
		return findDeviceList == null ? null : findDeviceList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i(TAG, "getView :"+position);
		Log.d(TAG, "findDeviceList = "+findDeviceList);
		if(findDeviceList == null){
			return null;
		}
		String deviceAddress = findDeviceList.get(position).getAddress();
		String deviceName = findDeviceList.get(position).getName();
        ViewHolder holder = new ViewHolder();
		
		if (convertView == null) 
		{
			LayoutInflater mInflater = LayoutInflater.from(ctx);
			convertView = mInflater.inflate(R.layout.bt_device_list_item, null);
			holder.deviceName = (TextView) convertView.findViewById(R.id.device_name);
			holder.deviceAddress = (TextView) convertView.findViewById(R.id.device_address);
			holder.btnSelect = (RadioButton) convertView.findViewById(R.id.device_select);
			holder.btnLayout = (LinearLayout) convertView.findViewById(R.id.bt_list_item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.deviceName.setText(deviceName);
		holder.deviceAddress.setText(deviceAddress);
		if(deviceAddress.equals(connectDeviceAddress)){
			holder.btnSelect.setChecked(true);
			holder.btnSelect.setVisibility(View.VISIBLE);
			holder.btnLayout.setBackgroundResource(R.color.white);
			holder.deviceName.setTextColor(ctx.getResources().getColor(R.color.app_theme_color_primary));
			holder.deviceAddress.setTextColor(ctx.getResources().getColor(R.color.app_theme_color_primary));
			
		}else{
			holder.btnSelect.setChecked(false);
			holder.btnSelect.setVisibility(View.INVISIBLE);
			holder.btnLayout.setBackgroundResource(R.color.white);
			holder.deviceName.setTextColor(ctx.getResources().getColor(R.color.gray));
			holder.deviceAddress.setTextColor(ctx.getResources().getColor(R.color.gray));
		}
		
		return convertView;
	}

	class ViewHolder {
		TextView deviceName;
		TextView deviceAddress;
		RadioButton btnSelect;
		LinearLayout btnLayout;
	}
	
	public void setConnectDeviceAddress(String mConnectDeviceAddress) {
		this.connectDeviceAddress = mConnectDeviceAddress;
	}
	public void setFindDeviceList(ArrayList<BluetoothDevice> findDeviceList) {
		this.findDeviceList = findDeviceList;
	}
}
